#include "featuresdialog.h"
#include "ui_featuresdialog.h"

#include <QFile>
#include <QFileDialog>
#include <QSettings>

FeaturesDialog::FeaturesDialog(QWidget *parent) :
		QDialog(parent),
		ui(new Ui::FeaturesDialog)
{
	ui->setupUi(this);
	ui->gb_watermarking->hide();
	ui->cb_signWatermark->hide();

	QSettings settings;

	int autopicture = settings.value("postfeatures/autopicture", 0).toUInt();
	switch(autopicture)
	{
	default:
	case AutopicDisable:
		ui->rb_apDisable->setChecked(true);
		break;
	case AutopicFromFs:
		ui->rb_apSpecificFs->setChecked(true);
		break;
	case AutopicFromDb:
		ui->rb_apSpecificDb->setChecked(true);
		break;
	case AutopicFromDbRandom:
		ui->rb_apRandom->setChecked(true);
		break;
	}

	m_pictureFilename = settings.value("postfeatures/filename").toString();
	ui->e_tags->setText(settings.value("postfeatures/tags").toString());
	ui->cb_randomAddendum->setChecked(settings.value("postfeatures/randomaddendum").toBool());
	ui->e_signature->setText(settings.value("postfeatures/signature").toString());
	ui->cb_signWatermark->setChecked(settings.value("postfeatures/injectsignature").toBool());

	int watermarking = settings.value("postfeatures/watermarking").toUInt();

	switch(watermarking)
	{
	default:
	case WatermarkDisable:
		ui->rb_wDisable->setChecked(true);
		break;
	case WatermarkWhitespace:
		ui->rb_wWhitespace->setChecked(true);
		break;
	case WatermarkText:
		ui->rb_wText->setChecked(true);
		break;
	case WatermarkImage:
		ui->rb_wImage->setChecked(true);
		break;
	}

	m_imageManager = new ImageManager(0);
	m_imageManager->hide();

	ui->e_tags->setEnabled(ui->rb_apRandom->isChecked());
	ui->l_pic->installEventFilter(this);
}

FeaturesDialog::~FeaturesDialog()
{
	delete m_imageManager;
	delete ui;
}

bool FeaturesDialog::eventFilter(QObject *obj, QEvent *event)
{
	if(event->type() != QEvent::MouseButtonPress)
		return false;
	if(obj == ui->l_pic)
	{
		selectPicture();
		return true;
	}
	return false;
}

void FeaturesDialog::changeEvent(QEvent *e)
{
	QDialog::changeEvent(e);
	switch (e->type()) {
	case QEvent::LanguageChange:
		ui->retranslateUi(this);
		break;
	default:
		break;
	}
}

void FeaturesDialog::accept()
{
	QSettings settings;
	int autopicture = AutopicDisable;
	if(ui->rb_apSpecificFs->isChecked())
	{
		if(!QFile::exists(m_pictureFilename))
			autopicture = AutopicDisable;
		else
			autopicture = AutopicFromFs;
	}
	if(ui->rb_apSpecificDb->isChecked())
		autopicture = AutopicFromDb;
	else if(ui->rb_apRandom->isChecked())
		autopicture = AutopicFromDbRandom;

	settings.setValue("postfeatures/autopicture", autopicture);
	settings.setValue("postfeatures/filename", m_pictureFilename);
	settings.setValue("postfeatures/tags", ui->e_tags->text());
	settings.setValue("postfeatures/randomaddendum", ui->cb_randomAddendum->isChecked());
	settings.setValue("postfeatures/signature", ui->e_signature->text());
	settings.setValue("postfeatures/injectsignature", ui->cb_signWatermark->isChecked());

	int watermarking = WatermarkDisable;
	if(ui->rb_wWhitespace->isChecked())
		watermarking = WatermarkWhitespace;
	else if(ui->rb_wText->isChecked())
		watermarking = WatermarkText;
	else if(ui->rb_wImage->isChecked())
		watermarking = WatermarkImage;
	settings.setValue("postfeatures/watermarking", watermarking);
	QDialog::accept();
}

void FeaturesDialog::updateStatus()
{

}

void FeaturesDialog::selectPicture()
{
	if(ui->rb_apSpecificFs->isChecked())
	{
		m_pictureFilename = QFileDialog::getOpenFileName(this, tr("Select picture..."), QString(), tr("Image Files (*.png *.jpg *.jpeg *.bmp *.gif)"));
		QPixmap p;
		if(!p.load(m_pictureFilename))
			return;
		ui->l_pic->setPixmap(p.scaled(ui->l_pic->size(), Qt::KeepAspectRatio));
	}
	else if(ui->rb_apSpecificDb->isChecked())
	{
		connect(m_imageManager, SIGNAL(pictureSelected(QString)), this, SLOT(pictureSelected(QString)));
		m_imageManager->setMode(true);
		m_pictureFilename = QString();
		m_imageManager->show();
	}
}

void FeaturesDialog::pictureSelected(const QString& filename)
{
	QPixmap p;
	if(!p.load(filename))
		return;
	m_pictureFilename = filename;
	ui->l_pic->setPixmap(p.scaled(ui->l_pic->size(), Qt::KeepAspectRatio));
}

void FeaturesDialog::randomPictureToggled(bool b)
{
	ui->e_tags->setEnabled(b);
}
